package org.example.onlineEducation.utils;

import org.example.onlineEducation.entity.User;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LoginUser implements Serializable, UserDetails, CredentialsContainer {

    private User user;
    //权限列表
    private List<MyGrantedAuthority> authorities = new ArrayList<>();

    public LoginUser(User user){
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setAuthorities(List<MyGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

//    public void setAuthorities(List<String> list) {
//        authorities.clear();
//        if(list == null)
//            return;
//        list.forEach(str -> this.authorities.add(new SimpleGrantedAuthority(str.toString())));
//    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }


    //重写方法默认为false, 需手动改为true才能使用
    @Override
    public boolean isAccountNonExpired() { return true;}
    @Override
    public boolean isAccountNonLocked() { return true;}
    @Override
    public boolean isCredentialsNonExpired() { return true;}
    @Override
    public boolean isEnabled() { return true;}

    //擦除凭证
    @Override
    public void eraseCredentials() {}

    public String toString(){
        return user.toString() + "\n" + "authorities: " + authorities.toString() + "\n";
    }
}
